-- chat example
-- server

require "server" -- include the server side library
print("started server.") -- make our server more than a blank window once we start it
running=true -- set a variable to control our while loop, even though we won't use it much
ip_binds={ } -- create a table so that we can bind ip addresses to usernames
net.start(6113) -- listen on port 6113
net.action("chat",function(data,sender) -- make it so that when we receive a chat message
	net.action("chat",ip_binds[net.ip(sender)].." "..data) -- we call the same action on all clients, formatting the message to use their username
	print(ip_binds[net.ip(sender)].." -> "..data) -- let the server see the message too
end)
net.action("setname",function(data,sender)
	ip_binds[net.ip(sender)]=data -- when someone joins, make an ip bind for them, so we can remember their username
	-- we could make the server call an action on clients that would display a message saying who joined, but i'll leave that for you to try!
	print(data.." joined the room") -- let us see who joined
end)
net.disconnect(function(ply)
	net.action("disconnect",ip_binds[net.ip(ply)]) -- call an action on clients telling them who disconnected
	print(ip_binds[net.ip(ply)].. " has disconnected.") -- let the server see who disconnected
	ip_binds[net.ip(ply)]="<disconnected>" -- change their ip bind so if we still see that we know something's gone wrong
	-- you will see this message when you leave if more than 1 person is connected from the same ip
	-- this is because the server sets the bind for their ip to disconnected when they leave, but there is another instance of them, so their username
	-- is basically changed.
end )
while(running) do
	net.process() -- this is the only server code that is executed during runtime!
	-- it will update our events, actions, streams, and let others join.
	-- in this chat example, we are only using actions.
	-- wasn't this a miniscule amount of code, especially for a chat server? [21 lines of code without all of the comments]
end
-- this lua code was compiled with l-bia 0.2.1
-- when compiling/running any of the libraries, the lua, mime, and socket folders must be in the same directory as your code file.